$(document).ready(init);

function init() {
	
	$('#add').click(addGroup);
	
	$('#search').click(doqueryGroup);
	
	queryGroup({});
}

function doqueryGroup() {
	var name = $.trim($('#appGroupName').val());
	var remark = $.trim($('#remark').val());
	
	var params = {};
	if  ('' != name) {
		params['apparatusGroup.appGroupName'] = name;
	}
	if  ('' != remark) {
		params['apparatusGroup.remark'] = remark;
	}

	queryGroup(params);
}

function queryGroup(params) {
	$('#appGroup').datagrid({
	    title:'仪器类别信息',
	    singleSelect:true,
	    url:'apparatus-group!queryApparatusGroup.action',
	    queryParams :params,
	    rownumbers : true,
	    idField: 'appGroupCode',
	    columns:[[
	        {field:'appGroupName',title:'仪器名称',width:fixWidth(0.12), editor:'text'},
	        {field:'remark',title:'分类描述',width:fixWidth(0.61), editor:'text'},
	        {field:'isUsed',title:'是否使用',width:fixWidth(0.11),
	        		 formatter : function(value, row, index) {
						if ('1' == value) {
							return '是';
				 		} else {
							var e = '否';
							return e;
						}
					},
					 editor: {type:"checkbox",options:{on:"1",off:"0"}}
	        },
	        {field:'appGroupCode',title:'操作',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, row, index) {
						if (row.editing) {
							var s = '<a href="#" style="color:red;text-decoration:none;" onclick="saverow('+ index + ',\'' + value +'\')">保存&nbsp;</a> ';
							var c = '<a href="#" style="color:red;text-decoration:none;" onclick="cancelrow('+index+')">取消</a>';
							return s + c;
				 		} else {
							var e = '<a href="#" style="color:red;text-decoration:none;" onclick="editrow('+index+')">编辑</a> ';
							return e;
						}
					}
			}
	    ]],
	    onBeforeEdit:function(index,row){
				row.editing = true;
				$('#appGroup').datagrid('refreshRow', index);
		},
		onAfterEdit:function(index,row){
				row.editing = false;
				$('#appGroup').datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,row){
				row.editing = false;
				$('#appGroup').datagrid('refreshRow', index);
		},
		pageList:[10],
	    pagination:true
	});
}


function editrow(index){
	$('#appGroup').datagrid('rejectChanges');
    $('#appGroup').datagrid('beginEdit', index);
}

function deleterow(index){
    $.messager.confirm('Confirm','Are you sure?',function(r){
        if (r){
            $('#appGroup').datagrid('deleteRow', index);
        }
    });
}

function saverow(index,value){
	$('#appGroup').datagrid('selectRow', index);
	$('#appGroup').datagrid('acceptChanges');
	
	var selected = $('#appGroup').datagrid('getSelected');

	var params = {
		'apparatusGroup.appGroupName' : selected['appGroupName'],
		'apparatusGroup.isUsed':selected['isUsed'],
		'apparatusGroup.remark' : selected['remark'],
		'apparatusGroup.appGroupCode' : selected['appGroupCode']
	};
	
	$.ajax({
		  type: 'POST',
		  url: 'apparatus-group!updateApparatusGroup.action',
		  data: params,
		  success: function(data) {
		  	if('success' == data) {
		  		$.messager.alert('确定', '<br/><b>恭喜您，修改成功</b>', 'info');
		  		
	       		$('#appGroup').datagrid('endEdit', index);
		  	} else {
		  		$.messager.alert('确定', '<br/><b>很遗憾，操作失败</b>', 'error');
		  		cancelrow(index);
		  	}
		  },
		  error:function(jqXHR, textStatus, errorThrown) {
		  	$.messager.alert('确定', '<br/><b>很遗憾，操作出现异常,状态为</b>'  + textStatus + ', 错误为： ' + errorThrown, 'error');
		  	cancelrow(index)
		  },
		  dataType: 'text'
	});
}

function cancelrow(index){
    $('#appGroup').datagrid('cancelEdit', index);
}

function addGroup() {
	var name = $.trim($('#appGroupName').val());
	
	if ('' == name) {
		$.messager.alert('确定', '<br/><b>对不起，分类名称不能为空</b>', 'error');
		
		return false;
	}
	
	var params = {
		'apparatusGroup.appGroupName' : name,
		'apparatusGroup.isUsed':1,
		'apparatusGroup.remark' : $.trim($('#remark').val())
	};
	
	$.ajax({
		  type: 'POST',
		  url: 'apparatus-group!addApparatusGroup.action',
		  data: params,
		  success: function(data) {
		  	if('success' == data) {
		  		$.messager.alert('确定', '<br/><b>恭喜您，添加成功</b>', 'info');
		  		
		  		queryGroup('{}');
		  	}
		  },
		  error:function(jqXHR, textStatus, errorThrown) {
		  	$.messager.alert('确定', '<br/><b>很遗憾，操作出现异常,状态为</b>'  + textStatus + ', 错误为： ' + errorThrown, 'error');
		  },
		  dataType: 'text'
	});
	
}